package com.sky.controller.user;

import com.sky.constant.StatusConstant;
import com.sky.entity.Dish;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController("userDishController")
@RequestMapping("/user/dish")
@Slf4j
public class DishController {

    @Autowired
    private RedisTemplate redisTemplate;

//    private static final

    @Autowired
    private DishService dishService;

    @GetMapping("/list")
    public Result<List<DishVO>> getDishWithFlavorByCategoryId(Long categoryId) {
        log.info("user query dish with flavor by categoryId:{}", categoryId);

        //先查redis
        String key="dish_"+categoryId;
        List<DishVO> dishVOList= (List<DishVO>) redisTemplate.opsForValue().get(key);
        if(dishVOList!=null&&dishVOList.size()>0){
            return Result.success(dishVOList);
        }

        //根据categoryId查询dish和dish相应的flavor
        //必须是起售的
        Dish dish = new Dish();
        dish.setCategoryId(categoryId);
        dish.setStatus(StatusConstant.ENABLE);

        dishVOList=dishService.getDishWithFlavor(dish);
        //放入缓存
        redisTemplate.opsForValue().set(key,dishVOList);

        return Result.success(dishVOList);
    }
}
